# pythonchallenge.com scripts
# challenge #6
# url: http://www.pythonchallenge.com/pc/def/channel.htm
# ref: http://docs.python.org/lib/module-zipfile.html

# our zip file
zfpath = 'C:\\PYTHON_ROOT\\pythonchallenge\\channel.zip'

# load modules
import zipfile
import re

# zipfile object
channel = zipfile.ZipFile(zfpath, 'r')


# ----------------------------------------------------------------------------
# List Files

# list file names
list_names = True
if list_names:
  for name in channel.namelist():
    print name,
    
#breakpoint()
    
# list file info
list_info = False
if list_info:
  for info in channel.infolist():
    print info.filename, info.date_time, info.file_size

# ----------------------------------------------------------------------------
# read functions

# def: read_zfile
def read_zfile(zipobj, zfname):
  return zipobj.read(zfname)
  
# def: print_zfile
def dump_zfile(zipobj, zfname):
  print read_zfile(zipobj, zfname)

  
# ----------------------------------------------------------------------------
# read readmi

# open readme
dump_zfile(channel, 'readme.txt')
#breakpoint()

# hint 1: start from 90052 
# hint 2: answer is inside the zip


# ----------------------------------------------------------------------------
# follow the nothings

dump_zfile(channel, '90052.txt')

# we have our pattern (see ch4)
next_nothing = '90052'
re1 = 'Next nothing is\s+([^\s]+)'
re2 = '#\s*([^\n]+)\n'

while next_nothing != '':

  # read file
  fname = next_nothing + '.txt'
  src = read_zfile(channel, fname)
  
  # look for nothings
  MatchNothing = re.compile(re1).search(src)
  
  # collect comments (this doesn't work)
  MatchComment = re.compile(re2).search(src)
  if MatchComment:
    print MatchComment.group(1)
    breakpoint("comment found")
  
  # dump contents?
  is_dump = True
  if is_dump: print repr(src)
  
  try:
    next_nothing = MatchNothing.group(1)
  except AttributeError:
    print 'parse failed -> break'
    print '\nlast file "' + next_nothing + '":\n' + src   # print last file
    break
    
  # collect the comments
    
if False: breakpoint()
    
# ----------------------------------------------------------------------------
# collect comments -- not these type of comments!

# set regex
re3 = re2  # see above

# test regex
if False:
  src = '''
# comment 1
var = exp
if True: False
#comment 3
'''

  MatchTest = re.compile(re3).findall(src)
  if MatchTest:
    for m in MatchTest: print 'matched: ' + m
  else:
    print "no match"
    
  breakpoint()
# end regex test

# let's just brute force the file list
COMMENT = []
str_sum = ''
fpath = 'C:\\PYTHON_ROOT\\pythonchallenge\\ch6.str_sum.txt'
fw = open(fpath, 'wb')

for fname in channel.namelist():

  # read file
  src = str(read_zfile(channel, fname))
  block = 'reading file "' + fname + '" ' + src + '\n-----------------\n'
  str_sum += block
  #fw.write(str(block))  
  
  # rip comments
  MatchComment = re.compile(re3).findall(src)
  if MatchComment:
    for m in MatchComment:
      COMMENT.append(m) 
      print m

if len(COMMENT) < 1: print "no comments found yet!"
else: 
  print "count: " + str(len(COMMENT))
  for c in COMMENT: print c 
  
fw.close

  
# ----------------------------------------------------------------------------
# collect ZipInfo comments -- see http://docs.python.org/lib/zipinfo-objects.html

ZCOMMENT = []
zsum = ''

for info in channel.infolist():
  c = info.comment
  print c,
  ZCOMMENT.append(c)
  
for c in ZCOMMENT:
  zsum += c
  
ZSET = set(ZCOMMENT)
set_sum = ''
for c in ZSET: set_sum += c
print set_sum

# let's look at in a file 
fpath = 'C:\\PYTHON_ROOT\\pythonchallenge\\ch6.zcomment.txt'
fw = open(fpath, 'w')
fw.write(str(zsum))
fw.close()
print zsum  


# ----------------------------------------------------------------------------
# combine techniques above: follow nothings, collecting comments
 
# a new function
def get_zcomment(ZipFile, zname):
  ZipInfo = ZipFile.getinfo(fname)
  c = ZipInfo.comment
  return c
  
re_nn = 'Next nothing is\s+([^\s]+)'
next_nothing = '90052'
COMMENT = []

while next_nothing != '':

  # get file src, comment
  fname = next_nothing + '.txt'
  src = read_zfile(channel, fname)
  c = get_zcomment(channel, fname)
  COMMENT.append(c)
  
  # look for nothings
  MatchNothing = re.compile(re_nn).search(src)
  
  # dump contents?
  is_dump = True
  if is_dump: print repr(src)
  
  try:
    next_nothing = MatchNothing.group(1)
  except AttributeError:
    print 'parse failed -> break'
    print '\nlast file "' + next_nothing + '":\n' + src   # print last file
    break
    
# a meaningful comment?
hint = ''
for c in COMMENT:
  hint += c
print hint

print "end ch6"
